<?php

namespace App\Admin\Controllers;

use App\Models\Manager;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ManagerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '管理者';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Manager());
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', __('Id'));
        $grid->column('name', __('Name'))->filter();;
        $grid->column('phone', __('Phone'));
        $grid->column('count_device', __('设备总计'));
        $grid->column('created_at', __('Created at'))->sortable();;
        $grid->column('updated_at', __('Updated at'))->sortable();;
//        $grid->expandFilter();
        $grid->filter(function ($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('name', __('Name'));
            $filter->equal('phone', __('Phone'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Manager::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('phone', __('Phone'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Manager());

        $form->text('name', __('Name'))->rules('required|min:2|max:6');
        $form->mobile('phone', __('Phone'))->rules('required|regex:/^1[345789][0-9]{9}$/|unique:managers');

        return $form;
    }
}
